package com.sskj.aikeeper.hangqing.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.RxPermissions;
import com.sskj.aikeeper.R;
import com.sskj.aikeeper.presenter.CertificationUploadPresenter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.widget.SelectPictureDialog;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.APP_ACTIVITY_CERTIFICATION_UPLOAD)
public class CertificationUploadActivity extends BaseActivity<CertificationUploadPresenter> {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.iv_hand)
    ImageView ivHand;
    @BindView(R.id.iv_font)
    ImageView ivFont;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @Autowired
    String identityName;
    @Autowired
    String identityCardId;

    private SelectPictureDialog selectPictureDialog;
    private String mHandImagePath;//手持身份证图片路径
    private String mFontImagePath;//身份证正面
    private String mBackImagePath;//身份证背面

    private int imageType;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_certification_upload;
    }

    @Override
    public CertificationUploadPresenter getPresenter() {
        return new CertificationUploadPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        tvTitle.setText(App.INSTANCE.getString(R.string.app_certificationActivity1));
        ivHand.setImageResource(R.mipmap.img_renzheng_hand);
        ivFont.setImageResource(R.mipmap.img_renzheng_font);
        ivBack.setImageResource(R.mipmap.img_renzheng_back);
    }

    @Override
    public void initData() {
        ClickUtil.click(300, btnConfirm, () -> {
            ARouter.getInstance().build(RConfig.APP_ACTIVITY_CERTIFICATION_RESULT).navigation();
        });
        ClickUtil.click(300, ivHand, () -> {
            imageType = 0;
            requestPermission();
        });
        ClickUtil.click(300, ivFont, () -> {
            imageType = 1;
            requestPermission();
        });
        ClickUtil.click(300, ivBack, () -> {
            imageType = 2;
            requestPermission();
        });
        ClickUtil.click(300, btnConfirm, () -> {
            if (isEmpty(mHandImagePath)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_certificationUploadActivity2));
                return;
            }
            if (isEmpty(mFontImagePath)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_certificationUploadActivity3));
                return;
            }
            if (isEmpty(mBackImagePath)) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_certificationUploadActivity4));
                return;
            }
            mPresenter.setRenZheng(identityName
                    , identityCardId
                    , new File(mHandImagePath)
                    , new File(mFontImagePath)
                    , new File(mBackImagePath));
        });
    }

    private void requestPermission() {
        new RxPermissions(CertificationUploadActivity.this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) {
                        selectPic();
                    }
                });
    }

    private void selectPic() {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(CertificationUploadActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(CertificationUploadActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .setOutputCameraPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        selectPictureDialog.show();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            switch (imageType) {
                case 0:
                    mHandImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
                    ImageUtil.setImage(mHandImagePath, ivHand);
                    break;
                case 1:
                    mFontImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
                    ImageUtil.setImage(mFontImagePath, ivFont);
                    break;
                case 2:
                    mBackImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
                    ImageUtil.setImage(mBackImagePath, ivBack);
                    break;
            }
        }
    }


    public static void start(Context context) {
        Intent intent = new Intent(context, CertificationUploadActivity.class);
        context.startActivity(intent);
    }
}
